/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *   02110-1301, USA.
 * 
 * $Id: EquipmentFrame.java 44 2008-05-14 18:22:13Z jens464 $
*/
package albareth.frontend.slick.ui;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;

import albareth.core.entity.Equipable;
import albareth.core.entity.EquipableItem;
import albareth.core.entity.Item;
import albareth.core.entity.PlayerCharacter;
import albareth.frontend.TileCollection;
import albareth.frontend.slick.SlickFrontend;
import albareth.frontend.slick.SlickResources;
import albareth.frontend.slick.state.InGameState;

public class EquipmentFrame extends UIElement
{
    private PlayerCharacter _player;
    private SlickFrontend _frontend;
    
    private float _scale = 2.0f;
    
    private static Vector2f[] _slotPositions = new Vector2f[] {
        new Vector2f(42, 4), //HEAD
        new Vector2f(42, 47), //ARMOR
        new Vector2f(42, 90), //WEAPON
        new Vector2f(237, 4), //AMULETTE
        new Vector2f(237, 47), //RING
        new Vector2f(237, 90), //SHIELD
};
    
    
    public EquipmentFrame(SlickFrontend frontend)
    {
        _frontend = frontend;
        _player = _frontend.getPlayerCharacter();
        setId(InGameState.COMP_EQUIPMENT);
        setForegroundColor(SlickFrontend.DEFAULT_TEXT_COLOR);
        setDraggingAllowed(true);
        setDroppingAllowed(true);
    }
    
    private int getSlotAtPixel(int x, int y)
    {
        float tileWidth = TileCollection.getDefaultTileWidth() * _scale;
        float tileHeight = TileCollection.getDefaultTileHeight() * _scale;
        
        for (int i = 0; i < PlayerCharacter.NUM_SLOTS; i++)
        {
            float leftX = _slotPositions[i].x;
            float topY = _slotPositions[i].y;
            float rightX = _slotPositions[i].x + tileWidth;
            float bottomY = _slotPositions[i].y + tileHeight;
            if (x >= leftX && x <= rightX && y >= topY && y <= bottomY)
            {
                return i;
            }
        }
        return -1;
    }
    
    
    public String getTooltipText(int x, int y)
    {
        int slot = getSlotAtPixel(x, y);
        if (slot > -1)
        {
            Equipable item = _player.getEquippedItem(slot);
            if (item != null) {
                return item.getName();
            }
        }
        return null;
    }

    public DraggeableItem getDraggeableItem(int x, int y)
    {
        int slot = getSlotAtPixel(x, y);
        if (slot > -1)
        {
            Item item = _player.getEquippedItem(slot);
            if (item != null) {
                DraggeableItem draggeable = 
                    SlickFrontend.createMenuItemFromEntity(item);
                draggeable.setOwnerComponent(this);
                return draggeable;
            }
        }
        return null;
    }

    public void render(Graphics g, GameContainer container, int screenX,
            int screenY) throws SlickException
    {
        g.drawImage(SlickResources.EQUIP_BG,screenX - 7, screenY - 25);
        
        //render equipped items:
        for (int i = 0; i < PlayerCharacter.NUM_SLOTS; i++)
        {
            //item icon:
            Equipable item = _player.getEquippedItem(i);
            if (item == null) {
                continue;
            }
            SlickFrontend.drawTile(
                    item.getTile(), 
                    screenX + _slotPositions[i].x,
                    screenY + _slotPositions[i].y, 
                    _scale, null, false
            ); 
        }
        
        //render avatar icon
        SlickFrontend.drawTile(
                _player.getTile(), 
                screenX + 140,
                screenY + 46, 
                _scale, null, false
        );
        
        //render stats:
        g.drawString("STR: " + _player.getStr(), screenX+1, screenY+130);
        g.drawString("DEX: " + _player.getDex(), screenX+1, screenY+150);
        g.drawString("INT: " + _player.getInt(), screenX+1, screenY+170);
        
        g.drawString(
                "HIT: " + _player.getTHAC0(), screenX+180, screenY+150);
        g.drawString(
                "DMG: " +
                _player.getMeleeDamageMin() + "-" + _player.getMeleeDamageMax(),
                screenX+180, screenY+130);
        g.drawString(
                "AC:  " + _player.getArmorClass(), screenX+180, screenY+170);

        
        
    }

    protected void itemDraggedAway(DraggeableItem draggedItem)
    {
    }

    protected boolean tryDropItem(DraggeableItem droppedItem, int x, int y)
    {
        if (!(droppedItem.getReferencedObject() instanceof Item)) {
            return false;
        }
        
        Item item = (Item)droppedItem.getReferencedObject();
        
        if (!item.isEquipable()) {
            return false;
        }

        boolean success = _player.tryEquip((EquipableItem)item);
        if (!success) {
            _player.infoMsg("You cannot equip this.");
        }
        
        return success;
    }
    
    
    

}
